<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Frontend extends CI_Controller {

    public $render_data;

    function __construct() {
        parent::__construct();
        $this->load->library('user_agent');
        $this->render_data['is_mobile'] = $this->agent->is_mobile();
    }

    public function index() {

        $this->load->library(array('template', 'carabiner'));
        $this->render_data['title'] = WEB_TITLE;

        $this->load->model('shop_model', 'shop');
        $this->render_data['shop_list'] = $this->shop->get_recent_approve_list();
        $this->render_data['headline'] = '<i class="icon-home"></i> New Shop (ร้านค้าล่าสุด)';
        $this->template->write_view('content', 'list_shop', $this->render_data);

        $this->render_data['shop_list'] = $this->shop->get_recent_updated_list();
        $this->render_data['headline'] = '<i class="icon-edit"></i> Recent Updated (ร้านค้าที่อัพเดทข้อมูลล่าสุด)';
        if ($this->render_data['shop_list'])
            $this->template->write_view('content', 'list_shop', $this->render_data);

        $this->template->write_view('main_nav', 'main_nav', $this->render_data);

        $this->template->render();
    }

    public function get_shop($shop_hash) {
        $shop_id = hashids_decrypt($shop_hash);
        if ($shop_id) {
            $this->load->model('shop_model', 'shop');
            $shop_fix = $this->shop->get_shop($shop_id);
            if ($shop_fix) {
                $this->render_data['shop_fix'] = $shop_fix;

                $this->render_data['og'] = array(
                    'title' => 'Shopsii - ' . $this->render_data['shop_fix']['shop_title'],
                    'url' => base_url() . 'shop/' . hashids_encrypt($this->render_data['shop_fix']['id']),
                    'image' => base_url() . 'uploads/shops/' . crc32_hash($this->render_data['shop_fix']['id']) . '/' . $this->render_data['shop_fix']['id'] . '/' . $this->render_data['shop_fix']['shop_level1_cover']
                );

                if ($this->render_data['is_mobile']) {
                    $this->load->library(array('template', 'carabiner'));
                    $this->render_data['headline'] = $this->render_data['shop_fix']['shop_title'];
                    $this->render_data['title'] = $this->render_data['shop_fix']['shop_title'] . ' | ' . WEB_TITLE;
                    $this->template->write_view('main_nav', 'main_nav', $this->render_data);
                    $this->template->write_view('content', 'mobile_get_shop', $this->render_data);
                    $this->template->render();
                } else {
                    $this->list_shop($shop_fix['shop_category']);
                }
            }
        } else {
            redirect('/');
        }
    }

    public function list_shop($category = '') {

        $this->load->library(array('template', 'carabiner'));
        $this->render_data['category'] = $category;
        switch ($category) {
            case 'women':
                $title = 'Women (คุณผู้หญิง)';
                $this->render_data['headline'] = '<i class="w-glyphicon-woman"></i> Women (คุณผู้หญิง)';
                break;
            case 'men':
                $title = 'Men (ท่านชาย)';
                $this->render_data['headline'] = '<i class="w-glyphicon-user"></i> Men (ท่านชาย)';
                break;
            case 'kids':
                $title = 'Kids (คุณหนู)';
                $this->render_data['headline'] = '<i class="w-glyphicon-girl"></i> Kids (คุณหนู)';
                break;

            case 'pets':
                $title = 'Pets (สัตว์เลี้ยง)';
                $this->render_data['headline'] = '<i class="w-glyphicon-dog"></i> Pets (สัตว์เลี้ยง)';
                break;
            case 'mobiles-it':
                $title = 'Mobiles & IT (มือถือและไอที)';
                $this->render_data['headline'] = '<i class="w-glyphicon-iphone"></i> Mobiles & IT (มือถือและไอที)';
                break;
            case 'gadgets-hobbies':
                $title = 'Gadgets & Hobbies (ของเล่นและงานอดิเรก)';
                $this->render_data['headline'] = '<i class="w-glyphicon-home"></i> Gadgets & Hobbies / ของเล่นและงานอดิเรก';
                break;

            default: redirect('/');
        }

        $this->load->model('shop_model', 'shop');
        $this->render_data['shop_list'] = $this->shop->get_shop_list($category);


        $this->render_data['title'] = $title . ' | ' . WEB_TITLE;
        $this->template->write_view('main_nav', 'main_nav', $this->render_data);
        $this->template->write_view('content', 'list_shop', $this->render_data);
        $this->template->render();
    }

}
